//create by lwy by 2017年11月11日14:16:59
var payMoney = GetQueryString('payMoney');
$('.money').val(payMoney);
var goodsInfo = JSON.parse(decodeURI(window.location.href.split('=')[3]));
$('.img img').attr('src',goodsInfo.goodsImg);
$('.name').html(goodsInfo.goodsName);
$('.guige').html(goodsInfo.guige);

//服务类型
var refundTypeValue = '';
var refundTypeHtml = '';
document.getElementById('choose').addEventListener('tap', function () {
    $('#stages_transaction').show();
    $('#stages_transaction2').hide();
    $('#title').html('服务类型');
    $('.stage_detail_box').empty();
    $(' <li class="mui-clearfix">\n' +
        '<div class="state_detail_img state_detail_img2"><img refundTypeValue="2" src="../phoneImg/no_pigeon.jpg" alt=""></div>\n' +
        '<div class="state_fr">\n' +
        '<span>仅退款</span><span>未收到货且在卖家协商同意的前提下</span>\n' +
        '</div></li><li class="mui-clearfix">\n' +
        '<div class="state_detail_img state_detail_img2"><img refundTypeValue="1" src="../phoneImg/no_pigeon.jpg" alt=""></div>\n' +
        '<div class="state_fr"><span>退货退款</span>\n' +
        '<span>已收到货，需要退换已收到的货物</span></div>\n' +
        '</li>').appendTo($('.stage_detail_box'))
    showBox('.stage_float_box', 200);
    $('.state_detail_img2 img').click(function () {
        if($(this).attr('src')=='../phoneImg/no_pigeon.jpg'){
            $('.state_detail_img img').attr('src','../phoneImg/no_pigeon.jpg');
            $(this).attr('src','../phoneImg/goods_pigeon.jpg');
            refundTypeValue = $(this).attr('refundTypeValue');
            refundTypeHtml = $(this).parent().siblings().find('span:nth-child(1)').html();
        }
    })
})

document.getElementById('close_stage').addEventListener('tap', function () {
    hideBox('.stage_float_box', 100);
})
document.getElementById('stages_transaction').addEventListener('tap', function (e) {
    $('#choose').html(refundTypeHtml);
    hideBox('.stage_float_box', 100);
    $('.submit').unbind('click');
    setTimeout(function () {
        $('.submit').bind('click',function () {
            submit();
        })
    },1000)
})

function white(event){
    var e = event || window.event;
    if (e && e.stopPropagation){
        e.stopPropagation();
    }else{
        e.cancelBubble=true;//兼容IE
    }
}


//退款原因
var refundReasonValue = '';
var refundReasonHtml = '';
document.getElementById('choose2').addEventListener('tap', function () {
    $('#title').html('退款原因');
    $('#stages_transaction2').show();
    $('#stages_transaction').hide();
    $('.stage_detail_box').empty();
    $('<li class="mui-clearfix">' +
        '<div class="state_detail_img state_detail_img3"><img refundReasonValue="2" src="../phoneImg/no_pigeon.jpg" alt=""></div>' +
        '<div class="state_fr"><span>我不想要了</span></div></li>' +
        '<li class="mui-clearfix">' +
        '<div class="state_detail_img state_detail_img3"><img refundReasonValue="2" src="../phoneImg/no_pigeon.jpg" alt=""></div>' +
        '<div class="state_fr"><span>收到商品破损</span></div></li>' +
        '<li class="mui-clearfix">' +
        '<div class="state_detail_img state_detail_img3"><img refundReasonValue="3" src="../phoneImg/no_pigeon.jpg" alt=""></div>' +
        '<div class="state_fr"><span>商品错发/漏发</span></div></li>' +
        '<li class="mui-clearfix">' +
        '<div class="state_detail_img state_detail_img3"><img refundReasonValue="4" src="../phoneImg/no_pigeon.jpg" alt=""></div>' +
        '<div class="state_fr"><span>商品需要维修</span></div></li>' +
        '<li class="mui-clearfix">' +
        '<div class="state_detail_img state_detail_img3"><img refundReasonValue="5" src="../phoneImg/no_pigeon.jpg" alt=""></div>' +
        '<div class="state_fr"><span>发票问题</span></div></li>' +
        '<li class="mui-clearfix">' +
        '<div class="state_detail_img state_detail_img3"><img refundReasonValue="6" src="../phoneImg/no_pigeon.jpg" alt=""></div>' +
        '<div class="state_fr"><span>收到商品与描述不符</span></div></li>' +
        '<li class="mui-clearfix">' +
        '<div class="state_detail_img state_detail_img3"><img refundReasonValue="7" src="../phoneImg/no_pigeon.jpg" alt=""></div>' +
        '<div class="state_fr"><span>做工瑕疵</span></div></li>' +
        '<li class="mui-clearfix">' +
        '<div class="state_detail_img state_detail_img3"><img refundReasonValue="8" src="../phoneImg/no_pigeon.jpg" alt=""></div>' +
        '<div class="state_fr"><span>商品质量问题</span></div></li>' +
        '<li class="mui-clearfix">' +
        '<div class="state_detail_img state_detail_img3"><img refundReasonValue="9" src="../phoneImg/no_pigeon.jpg" alt=""></div>' +
        '<div class="state_fr"><span>未按约定时间发货</span></div></li>' +
        '<li class="mui-clearfix">' +
        '<div class="state_detail_img state_detail_img3"><img refundReasonValue="10" src="../phoneImg/no_pigeon.jpg" alt=""></div>' +
        '<div class="state_fr"><span>假冒品牌</span></div></li>' +
        '<li class="mui-clearfix">' +
        '<div class="state_detail_img state_detail_img3"><img refundReasonValue="11" src="../phoneImg/no_pigeon.jpg" alt=""></div>' +
        '<div class="state_fr"><span>商品补偿</span></div></li>').appendTo($('.stage_detail_box'));
    mui('body').on('tap','.state_detail_img3 img',function () {
        if($(this).attr('src')=='../phoneImg/no_pigeon.jpg'){
            $('.state_detail_img3 img').attr('src','../phoneImg/no_pigeon.jpg');
            $(this).attr('src','../phoneImg/goods_pigeon.jpg');
            refundReasonValue = $(this).attr('refundReasonValue');
            refundReasonHtml = $(this).parent().siblings().find('span').html();
        }
    })

    showBox('.stage_float_box', 200);
})

document.getElementById('close_stage').addEventListener('tap', function () {
    hideBox('.stage_float_box', 100);
})
document.getElementById('stages_transaction2').addEventListener('tap', function (e) {
    $('#choose2').html(refundReasonHtml);
    hideBox('.stage_float_box', 100);
    $('.submit').unbind('click');
    setTimeout(function () {
        $('.submit').bind('click',function () {
            submit();
        })
    },1000)
    return;
})

$('.submit').bind('click',function () {
    submit();
})

function submit() {
    $.ajax({
        url:'/purchaseOrderC/refundApply.json',
        dataType:'json',
        data:{
            token:getItem('token'),
            goodsOrderId:GetQueryString('goodsOrderId'),
            refundTypeValue:refundTypeValue,
            refundReasonValue:refundReasonValue,
            refundMoney:$('.money').val(),
            refundExplain:$('#refundExplain').val(),
            picture:$('.goods_img').attr('goodsImg'),
            companyCode:getItem('companyCode'),
        },
        type:'post',
        success:function (data) {
            if(data.code==1){
                mui.alert('申请成功',function () {
                    window.location.href="order.html?orderStateValue=12"
                })
            }
            if(data.code==-1){
                window.location.href="login.html"
            }
            if(data.code==0){
                mui.toast(data.msg);
                return;
            }
        }
    })
}

//商品上传图片
$(function () {
    if(ifApp()==0||ifApp()==1){
        $('body').on('change', '.file_input', function () {
            var loadId = $(this).attr('id');
            changeImg2(loadId);
        })
    }else{
        //android
        $('body').on('click', '.file_input', function () {
            window.ddw.gopingjia('3');
        })
    }
})


function chaImage(id,url) {
    var imgUrl = url.split(',');
    var imgs2 = '';
    var oDiv = $('#' + id + '').parent().parent().find('img');
    var imgUrlLenth = imgUrl.length-1;
    for(var i=0;i<imgUrlLenth;i++){
        //alert($(oDiv[i]));
        $(oDiv[i]).attr('src',imgUrl[i]);
        $(oDiv[i]).show();
        $(oDiv[parseInt(i+1)]).show();
    }
    $(oDiv).each(function () {
        $(oDiv[imgUrlLenth]).show();
        return;
    })
    var fileHeight = $('.goods_img').height()-10;
    $('.file_input').css('height',fileHeight+'px');
    var goodsImg = $('#' + id + '').parent().parent().attr('goodsImg');
    if(goodsImg==""){}else{
        goodsImg = goodsImg+url
    }
    $('#' + id + '').parent().parent().attr('goodsImg', url);
}



function changeImg2(a) {
    var imgs2 = "";
    var IMG2 = 0;
    if (IMG2 > 4) {
        mui.alert('图片已上传完毕！');
        return;
    } else {
        selectOver2(a);
    }

    function selectOver2(a) {
        IMG2++;
        $.ajaxFileUpload({
            url: '/uploadImage.do',
            type: 'POST',
            fileElementId: a,
            dataType: 'JSON',
            success: function (data) {
                var results2 = data.split('|');
                var flag2 = results2[0].split('>');
                if (flag2[1] != 'success') {
                    mui.alert('上传失败');
                    IMG2--;
                    return;
                }

                var img2 = results2[1].substring(results2[1].length - 6, 0);
                IMG2++;

                var imgBox = $('#' + a + '').parent().parent().find('img');
                if ($(imgBox[0]).attr('src') == '../phoneImg/refund-img.jpg') {
                    $(imgBox[0]).attr('src', img2);
                    imgs2 = imgs2 + $(imgBox[0]).attr("src");
                    $(imgBox[1]).attr("src", "../phoneImg/refund-img.jpg");
                    $(imgBox[1]).css('display', 'block');
                } else if ($(imgBox[1]).attr('src') == '../phoneImg/refund-img.jpg') {
                    $(imgBox[1]).attr('src', img2);
                    imgs2 = imgs2 + "," + $(imgBox[1]).attr("src");
                    $(imgBox[2]).attr("src", "../phoneImg/refund-img.jpg");
                    $(imgBox[2]).css('display', 'block');
                } else if ($(imgBox[2]).attr('src') == '../phoneImg/refund-img.jpg') {
                    $(imgBox[2]).attr('src', img2);
                    imgs2 = imgs2 + "," + $(imgBox[2]).attr("src");
                    $(imgBox[3]).attr("src", "../phoneImg/refund-img.jpg");
                    $(imgBox[3]).css('display', 'block');
                } else if ($(imgBox[3]).attr('src') == '../phoneImg/refund-img.jpg') {
                    $(imgBox[3]).attr('src', img2);
                    imgs2 = imgs2 + "," + $(imgBox[4]).attr("src");
                    $(imgBox[4]).attr("src", "../phoneImg/refund-img.jpg");
                    $(imgBox[4]).css('display', 'block');
                } else if ($(imgBox[4]).attr("src") == "" || $(imgBox[4]).attr('src') == '../phoneImg/refund-img.jpg') {
                    $(imgBox[4]).attr('src', img2);
                    imgs2 = imgs2 + "," + $(imgBox[4]).attr("src");
                }
                imgs2 = '';
                $('.goods_img img').each(function () {
                    if($(this).attr('src')!='../phoneImg/refund-img.jpg'){
                        imgs2+=$(this).attr('src')+',';
                    }
                })
                $('#' + a + '').parent().parent().attr('goodsImg', imgs2);

            },
            error: function (data) {
                IMG2--;
                mui.alert('上传失败');

            }
        })
    }
}
